using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Utilities;
using UnityEngine.UI;


public enum BTN_TYPE
{
    UP,
    DOWN,
    LEFT,
    RIGHT,
    JUMP,
    FIRE
}

public class ConfigPanel : MonoBehaviour
{
    private static ConfigPanel instance;
    public static ConfigPanel Instance => instance;
    private CanvasGroup canvasGroup;
    public Text txtUp;
    public Button btnUp;

    public Text txtDown;
    public Button btnDown;

    public Text txtLeft;
    public Button btnLeft;

    public Text txtRight;
    public Button btnRight;

    public Text txtFire;
    public Button btnFire;

    public Text txtJump;
    public Button btnJump;

    public Button btnClose;

    private InputConf inputConf;
    //记录当前修改哪一个键
    private BTN_TYPE nowType;

    public Lesson16_Player player;

    void Awake()
    {
        instance = this;
        canvasGroup = this.GetComponent<CanvasGroup>();
        if (canvasGroup == null)
            canvasGroup = this.gameObject.AddComponent<CanvasGroup>();
        Hide();
    }

    public void Show()
    {
        if (canvasGroup == null)
            return;
        canvasGroup.alpha = 1;
        player.CloseInput();
    }

    public void Hide()
    {
        if (canvasGroup == null)
            return;
        canvasGroup.alpha = 0;
        player.OpenInput();
    }

    void Start()
    {
        inputConf = DataManger.Instance.InputConf;
        UpInputConfUI();

        btnUp.onClick.AddListener(() =>
        {
            ChangeBtn(BTN_TYPE.UP);
        });
        btnDown.onClick.AddListener(() =>
        {
            ChangeBtn(BTN_TYPE.DOWN);
        });
        btnLeft.onClick.AddListener(() =>
        {
            ChangeBtn(BTN_TYPE.LEFT);
        });
        btnRight.onClick.AddListener(() =>
        {
            ChangeBtn(BTN_TYPE.RIGHT);
        });
        btnFire.onClick.AddListener(() =>
        {
            ChangeBtn(BTN_TYPE.FIRE);
        });
        btnJump.onClick.AddListener(() =>
        {
            ChangeBtn(BTN_TYPE.JUMP);
        });
        btnClose.onClick.AddListener(()=>
        {
            this.Hide();
        });
    }

    private void ChangeBtn(BTN_TYPE type)
    {
        nowType = type;
        InputSystem.onAnyButtonPress.CallOnce(ChangeBtnReally);
    }

    private void ChangeBtnReally(InputControl control)
    {
        string[] strs = control.path.Split('/');
        string path = string.Format("<{0}>/{1}", strs[1], strs[2]);
        switch (nowType)
        {
            case BTN_TYPE.UP:
                inputConf.up = path;
                break;
            case BTN_TYPE.DOWN:
                inputConf.down = path;
                break;
            case BTN_TYPE.LEFT:
                inputConf.left = path;
                break;
            case BTN_TYPE.RIGHT:
                inputConf.right = path;
                break;
            case BTN_TYPE.JUMP:
                inputConf.jump = path;
                break;
            case BTN_TYPE.FIRE:
                inputConf.fire = path;
                break;
            default:
                break;
        }
        UpInputConfUI();
        player.OnChangeInputConf();
    }

    //更新键盘显示信息
    private void UpInputConfUI()
    {
        txtUp.text = inputConf.up;
        txtDown.text = inputConf.down;
        txtLeft.text = inputConf.left;
        txtRight.text = inputConf.right;

        txtFire.text = inputConf.fire;
        txtJump.text = inputConf.jump;
    }
}
